// 挂在硬砖块上，防止角色穿透墙壁
// 
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.gameScene = ck.getGameScene()
    },

    // 只负责角色和砖块的碰撞
    onCollisionEnter: function (playerCollider, self) {

        if (this.gameScene.data.state === ck.GameState.Prepare)
            return

        if (this.gameScene.data.state === ck.GameState.TouchDoor)
            return

        if (this.gameScene.data.state === ck.GameState.Finished)
            return

        if (this.gameScene.data.state === ck.GameState.Dead)
            return

        var player = playerCollider.node.getComponent('Player')


        // 1st step 
        // get pre aabb, go back before collision
        var playerAabb = playerCollider.world.aabb;
        var playerPreAabb = playerCollider.world.preAabb.clone();

        var selfAabb = self.world.aabb;
        var selfPreAabb = self.world.preAabb.clone();

        // 2nd step
        // forward x-axis, check whether collision on x-axis
        selfPreAabb.x = selfAabb.x;
        playerPreAabb.x = playerAabb.x;

        if (cc.Intersection.rectRect(selfPreAabb, playerPreAabb)) {
            if (player.getHDirection()===ck.PlayerDirection.Left) {
                player.setHDirection(ck.PlayerDirection.Right);
            }
            else if (player.getHDirection()===ck.PlayerDirection.Right) {
                player.setHDirection(ck.PlayerDirection.Left);
            }
            // ck.log('collision', 'brick.x')
            return
        }

        // 3rd step
        // forward y-axis, check whether collision on y-axis
        selfPreAabb.y = selfAabb.y;
        playerPreAabb.y = playerAabb.y;

        if (cc.Intersection.rectRect(selfPreAabb, playerPreAabb)) {
            ck.log('collision', 'brick.y')
            if (player.getVDirection()===ck.PlayerDirection.Down) {
                player.putColliderAbovePlatformWorldY(selfAabb.yMax)
                this.resetPlayerDropDown = true
            }
            else if (player.getVDirection()===ck.PlayerDirection.Up) {
                player.putColliderUnderWorldY(selfAabb.yMin)
                player.stopJump()
            }
        }
    },

    onCollisionExit(playerCollider, self) {

        if (this.resetPlayerDropDown) {
            var player = playerCollider.node.getComponent('Player')
            player.setNeedDropDown(true)
            
            this.resetPlayerDropDown = false
        }
    },
});
